package com.shazhu.nongjt.service.impl;

import java.util.Date;
import java.util.List;

import com.shazhu.common.core.domain.AjaxResult;
import com.shazhu.common.core.domain.entity.SysRole;
import com.shazhu.common.core.domain.entity.SysUser;
import com.shazhu.common.enums.SysRoleEnum;
import com.shazhu.common.enums.YesOrNoEnum;
import com.shazhu.common.utils.DateUtils;
import com.shazhu.common.utils.SecurityUtils;
import com.shazhu.system.service.ISysRoleService;
import com.shazhu.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shazhu.nongjt.mapper.NjtUserRegisterMapper;
import com.shazhu.nongjt.domain.NjtUserRegister;
import com.shazhu.nongjt.service.INjtUserRegisterService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户注册信息Service业务层处理
 * 
 * @author shazhu
 * @date 2025-09-19
 */
@Service
public class NjtUserRegisterServiceImpl implements INjtUserRegisterService 
{
    @Autowired
    private NjtUserRegisterMapper njtUserRegisterMapper;

    /**
     * 查询用户注册信息
     * 
     * @param id 用户注册信息主键
     * @return 用户注册信息
     */
    @Override
    public NjtUserRegister selectNjtUserRegisterById(Long id)
    {
        return njtUserRegisterMapper.selectNjtUserRegisterById(id);
    }

    /**
     * 查询用户注册信息列表
     * 
     * @param njtUserRegister 用户注册信息
     * @return 用户注册信息
     */
    @Override
    public List<NjtUserRegister> selectNjtUserRegisterList(NjtUserRegister njtUserRegister)
    {
        return njtUserRegisterMapper.selectNjtUserRegisterList(njtUserRegister);
    }

    /**
     * 新增用户注册信息
     * 
     * @param njtUserRegister 用户注册信息
     * @return 结果
     */
    @Override
    public int insertNjtUserRegister(NjtUserRegister njtUserRegister)
    {
        njtUserRegister.setCreateTime(DateUtils.getNowDate());
        return njtUserRegisterMapper.insertNjtUserRegister(njtUserRegister);
    }

    /**
     * 修改用户注册信息
     * 
     * @param njtUserRegister 用户注册信息
     * @return 结果
     */
    @Override
    public int updateNjtUserRegister(NjtUserRegister njtUserRegister)
    {
        njtUserRegister.setUpdateTime(DateUtils.getNowDate());
        return njtUserRegisterMapper.updateNjtUserRegister(njtUserRegister);
    }

    /**
     * 批量删除用户注册信息
     * 
     * @param ids 需要删除的用户注册信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserRegisterByIds(Long[] ids)
    {
        return njtUserRegisterMapper.deleteNjtUserRegisterByIds(ids);
    }

    /**
     * 删除用户注册信息信息
     * 
     * @param id 用户注册信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserRegisterById(Long id)
    {
        return njtUserRegisterMapper.deleteNjtUserRegisterById(id);
    }
    /**
     * 根据⼿机号查询会员信息
     *
     * @param mobilePhone
     * @return
     */
    @Override
    public NjtUserRegister getByMobilePhone(String mobilePhone) {
        NjtUserRegister userRegister = new NjtUserRegister();
        userRegister.setMobilePhone(mobilePhone);
        List<NjtUserRegister> registerList = selectNjtUserRegisterList(userRegister);
        if (null != registerList && !registerList.isEmpty()) {
            return registerList.get(0);
        }
        return null;
    }
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleService sysRoleService;
    /**
     * 会员提交注册
     *
     * @param mobilePhone
     * @param password
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveRegister(String mobilePhone, String password) {
        // 再次判断⼿机号是否被注册，避免注册过程中被其他⽤户注册情况
        NjtUserRegister register = this.getByMobilePhone(mobilePhone);
        if (null != register) {
            return AjaxResult.error("该⼿机号已被注册,请更换注册⼿机号");
        }
        // 密码加密
        String encryptPassword = SecurityUtils.encryptPassword(password);
        // 创建并保存注册⽤户信息
        NjtUserRegister userRegister = new NjtUserRegister();
        userRegister.setMobilePhone(mobilePhone);
        // 密码使⽤md5加密存储
        userRegister.setPassword(encryptPassword);
        // 会员状态，默认⾮会员
        userRegister.setMemberFlag(YesOrNoEnum.NO.getCode());
        // 完善状态，默认未完善
        userRegister.setPerfectFlag(YesOrNoEnum.NO.getCode());
        // ⽤户状态，默认可⽤
        userRegister.setUserStatus(YesOrNoEnum.YES.getCode());
        userRegister.setRegisterTime(new Date());
        // 删除标志，默认未删除
        userRegister.setDelFlag(YesOrNoEnum.NO.getCode());
        // 保存注册⽤户信息
        insertNjtUserRegister(userRegister);
        // 创建并保存系统⽤户信息
        SysUser sysUser = new SysUser();
        sysUser.setUserName(mobilePhone);
        sysUser.setNickName(mobilePhone);
        sysUser.setPassword(encryptPassword);
        // 默认⽤户⻆⾊为普通⽤户
        SysRole sysRole = sysRoleService.getByRoleKey(SysRoleEnum.COMMON.getRoleCode());
        if (null != sysRole) {
            sysUser.setRoleIds(new Long[]{sysRole.getRoleId()});
        }
        // 默认⽤户类型为普通⽤户，后期定义成枚举类，数据权限控制时定义的⽤户类型枚举

        sysUserService.insertUser(sysUser);
        return AjaxResult.success("注册成功");
    }
    /**
     * 忘记或修改密码
     *
     * @param mobilePhone
     * @param password
     * @return
     */
    @Override
    public AjaxResult updateRegister(String mobilePhone, String password) {
        // 再次判断⼿机号是否被注册，避免注册过程中被其他⽤户注册情况
        NjtUserRegister register = this.getByMobilePhone(mobilePhone);
        if (null == register) {
            return AjaxResult.error("该⼿机号未注册,请注册⼿机号");
        }
        // 密码加密
        String encryptPassword = SecurityUtils.encryptPassword(password);
        // 密码使⽤md5加密存储
        register.setPassword(encryptPassword);
        // 修改注册⽤户信息
        this.updateNjtUserRegister(register);
        //查询系统⽤户信息
        SysUser user = sysUserService.selectUserByUserName(mobilePhone);
        user.setPassword(encryptPassword);
        sysUserService.updateUser(user);
        return AjaxResult.success("修改成功");
    }
}
